#!/bin/bash
# By tzdtwsj.


check_depends() {
status=1
echo "依赖检查中"
for i in $@; do
if test "$(command -v $i)" = "";then
	echo "找不到命令$i"
	status=0
fi
done
if test "$status" = "0"; then
	echo "请安装完这些依赖后再继续"
	exit 1
fi
}

check_depends curl grep sed rm

HTML_DATA="$(curl -L https://www.minecraft.net/zh-hans/download/server -A "Mozilla/5.0 (Linux; Android 12; Phone) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/113.0.0.0 Mobile Safari/537.36 EdgA/113.0.1774.38"|grep -E "minecraft_server\..*?\.jar<")"

if test "$HTML_DATA" = ""; then
	echo "获取数据失败"
	exit 1
fi

VANILLA_LINK="$(echo $HTML_DATA|grep -E "minecraft_server\..*?\.jar<"|sed -e 's/.*http/http/' -e 's/\.jar" a.*/\.jar/')"

FILENAME="$(echo $HTML_DATA|grep -E "minecraft_server\..*?\.jar<"|sed -e 's/.*n">m/m/' -e 's/r<.*/r/g')"

MC_VERSION="$(echo $FILENAME|sed -e 's/.*er\.//' -e 's/\.jar//')"

echo 从官网找到MC版本$MC_VERSION
echo 下载中
curl -L "$VANILLA_LINK" -o server.jar
if test "$?" != "0"; then
	echo "服务端下载失败"
	rm -rf server.jar
	exit 1
fi
echo "服务端下载成功！"
echo "你是否同意minecraft最终用户许可协议(eula)？同意将会自动写入eula.txt"
read -p ":(Y/n)" input
if test "$?" != "0"; then input=y; fi
case "$input" in
n|no) echo "你拒绝了eula";;
*) echo "eula=true" > eula.txt;;
esac
